package com.evil.kingdom.interceptor;

import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * 属性锁
 *
 * @author liyang
 * @date 2024-07-01 18:34
 */
@Aspect
@Component
@AllArgsConstructor
public class LockFieldAspect {

    private final RedissonClient redissonClient;

    @Pointcut(value = "execution(* com.evil.kingdom..*.get*(..)) && @annotation(lockField)", argNames = "joinPoint,lockField")
    public void getterMethod(ProceedingJoinPoint joinPoint, LockField lockField) {
    }

    @Pointcut(value = "execution(* com.evil.kingdom..*.set*(..)) && @annotation(lockField)", argNames = "joinPoint,lockField")
    public void setterMethod(ProceedingJoinPoint joinPoint, LockField lockField) {
    }

    /**
     * 环绕getter方法
     *
     * @param joinPoint joinPoint
     * @param lockField lockField
     * @return Object
     */
    @Around(value = "getterMethod(joinPoint, lockField)", argNames = "joinPoint,lockField")
    public Object aroundGetter(ProceedingJoinPoint joinPoint, LockField lockField) throws Throwable {
        // 这里可以执行你需要的逻辑
        return joinPoint.proceed();
    }

    /**
     * 环绕setter方法
     *
     * @param joinPoint joinPoint
     * @param lockField lockField
     * @return Object
     */
    @Around(value = "setterMethod(joinPoint, lockField)", argNames = "joinPoint,lockField")
    public Object aroundSetter(ProceedingJoinPoint joinPoint, LockField lockField) throws Throwable {
        // 这里可以执行你需要的逻辑
        return joinPoint.proceed();
    }

    // 辅助方法，用于检查字段是否带有@CustomProperty注解
    @SuppressWarnings("unused")
    private LockField getCustomProperty(Field field) {
        return field.getAnnotation(LockField.class);
    }
}
